﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#if UNITY_EDITOR
[ExecuteInEditMode]
public class LinePointToMesh : MonoBehaviour
{
    public LineRenderer lineRender;
    public int smooth = 30;
    public bool bUpdateMash = false;
    public float lineWidth = 1;
    public float y = 0.1f;

    void Start()
    {
        if (lineRender == null) lineRender = gameObject.GetOrAddComponent<LineRenderer>();
    }

    private void Update()
    {
        if (bUpdateMash)
        {
            DrawLineMesh();
        }
    }

    public void DrawLineMesh()
    {
        if (lineRender)
        {
            List<Vector3> listPoint = new List<Vector3>();
            for (int i = 0; i < transform.childCount; i++)
            {
                Vector3 p0 = transform.GetChild(i).position;
                listPoint.Add(p0);
            }
  
            lineRender.positionCount = listPoint.Count;
            lineRender.startWidth = lineWidth;
            lineRender.endWidth = lineWidth;
            for (int i = 0; i < listPoint.Count; i++)
            {
                Vector3 pos = listPoint[i];
                pos.y = y;
                lineRender.SetPosition(i, pos);
            }

        }
    }
}

#endif
